<?php

/**
 * @Author: king
 * @Date:   2019-04-27 16:33:27
 * @Last Modified by:   king
 * @Last Modified time: 2019-04-27 16:43:25
 */


namespace App\Http\Controllers\Api\v2;

use App\Http\Controllers\Api\BaseController;
use App\Tools\Astrolog\Astrolog;
use Illuminate\Http\Request;

//use app\api\controller\v2\Auth;
use App\Models\Api\AstroPhaseText;
use App\Models\Api\AstroSignText;
use App\Models\Api\Signtext;

use App\Models\Api\Recode;

class AstroExplainController extends BaseController
{
    protected $phaseTextModel;
    protected $signTextModel;
    protected $AppSignTextModel;

    public function __construct()
    {
        parent::__construct();
        $this->phaseTextModel = new AstroPhaseText();
        $this->signTextModel = new AstroSignText();
        $this->AppSignTextModel = new Signtext();
    }

    /**获取星盘解读数据
     * @return \think\response\Json
     */
    public function index(Request $request)
    {
        if (!Request()->isMethod('get')) {
            return show(config('code.error'), '未知操作', '', 403);
        }
        $pid = $request->input('pid',0);
        $rid = $request->input('rid',0);
        if (!$rid) {
            return show(config('code.error'), '当前档案不存在');
        }
        // 获取当前档案的行星数据


        if ($rid) {
            $map = ['id' => $rid];
        }

        $RecodeModel = new Recode();

        $userinfo = $RecodeModel->getRecode($map);
        $natalData = (new Astrolog)->setRecode1($userinfo)
            ->setExecPrefix(config('astrolog.prefix'))
            ->setIsAstrolabe(false)
            ->setUserSetting(config('astroset.setting1'))
            ->setGetDatas(['natal'])
            ->getAstroDatas()
            ->getDatas('natal');



//        // 检测网路连接
        //$link_status = varify_url('https://www.baidu.com');
        $link_status ='1';
        $planet = [];
        $phase_data = [];
        $array = [];

        // 获取行星配置信息
        $planet_info = config('astro');
        $planet_name = $planet_info['planet_name'];
        $planet_des = $planet_info['planet_des'];
        $planet_img = $planet_info['planet_img'];
        $planet_color = $planet_info['planet_color'];
        $planet_header_color = $planet_info['planet_header_color'];
        // 获取行星落宫落座图标和名称
        $sign_icon = $planet_info['sign_icon'];
        $house_icon = $planet_info['house_icon'];
        $sign_name = $planet_info['sign_name'];
        // 获取纤维图标和名称
        $phase_icon1 = $planet_info['phase_icon'];
        $phase_icon2 = $planet_info['phase_icon'];
        $phase_name1 = $planet_info['planet_name'];
        $phase_name2 = $planet_info['planet_name'];

        // 从数据库获取所有解读词条
        $sign_text = $this->AppSignTextModel->getAllSignText();
        $house_text = $this->signTextModel->getAllHouseText();
        $phase_text = $this->phaseTextModel->getAllPhaseText();

        $data = [];
        // 获取解读词条
        foreach ($natalData['planets']  as $k => $v) {
            if ($v['id'] < 10) {
                // 获取行星信息
                $array['planet']['id'] = $v['id'];
                $array['planet']['name'] = $planet_name[$v['id']];
                $array['planet']['des'] = $planet_des[$v['id']];
                $array['planet']['img'] = $planet_img[$v['id']];
                $array['planet']['color'] = $planet_color[$v['id']];
                $array['planet']['header_color'] = $planet_header_color[$v['id']];
                // 获取行星落宫落座图标和名称
                $array['sign']['icon'] = $sign_icon[$v['in_sign_id']];
                $array['house']['icon'] = $house_icon[$v['in_house_id']];
                $array['sign']['name'] = $sign_name[$v['in_sign_id']];
                $array['house']['name'] = $v['in_house_id'];
                 //var_dump($array);
                // 获取行星落宫落座词条
                $sign_data = [];
                foreach ($sign_text as $sign) {
                    if ($v['in_sign_id'] == $sign['sign_id'] && $v['id'] == $sign['planet_id']) {
                        $sign_data[] = $sign['text'];
                    }

                }
                $array['sign']['text'] = $sign_data;
                $house_data = [];
                foreach ($house_text as $house) {
                    if ($v['in_house_id'] == $house['tid'] && $v['id'] == $house['planet']) {

                        $last_str = mb_substr($house['text'], -1);
                        if ($last_str == '。') {
                            $house['text'] = mb_substr($house['text'], 0, -1) . '；';
                        } elseif (mb_substr($house['text'], -1) != '；') {
                            $house['text'] .= '；';
                        }
                        $house_data[] = $house['text'];
                    }
                }
                $array['house']['text'] = $house_data;

//                $array['sign']['text'] = $this->AppSignTextModel->getSignText(['sign_id'=>$v['in_sign_id'],'planet_id'=>$v['id']]);
//                $array['house']['text'] = $this->signTextModel->getHouseText(['house_id'=>$v['in_house_id'],'planet_id'=>$v['id']]);

                $array['phase'] = [];
                $degrees = 0;
                if (is_array($v['phase']) && !empty($v['phase'])) {
                    foreach ($v['phase'] as $kk => $vv) {
                        $planet = explode('-', $kk);

                        if ($planet[0] < 10 && $planet[1] < 10) {
                            // 获取行星相位图标和名称
                            $phase_data['icon1'] = $phase_icon1[$planet[0]];
                            $phase_data['icon2'] = $phase_icon2[$planet[1]];
                            $phase_data['name1'] = $phase_name1[$planet[0]];
                            $phase_data['name2'] = $phase_name2[$planet[1]];
                            $phase_data['degrees'] = $vv['phase_e'];

                            switch ($vv['phase_e']) {
                                case 0:
                                    $degrees = 1;
                                    break;
                                case 60:
                                    $degrees = 2;
                                    break;
                                case 90:
                                    $degrees = 3;
                                    break;
                                case 120:
                                    $degrees = 4;
                                    break;
                                case 180:
                                    $degrees = 5;

                            }
                            // 获取行星相位词条

                            $phase_array = [];
                            foreach ($phase_text as $phase) {
                                if (($planet[0] == $phase['planet1'] && $planet[1] == $phase['planet2'] && $degrees == $phase['degrees']) ||
                                    ($planet[0] == $phase['planet2'] && $planet[1] == $phase['planet1'] && $degrees == $phase['degrees'])) {

                                    $last_str = mb_substr($phase['text'], -1);
                                    if ($last_str == '。') {
                                        $phase['text'] = mb_substr($phase['text'], 0, -1) . '；';
                                    } elseif (mb_substr($phase['text'], -1) != '；') {
                                        $phase['text'] .= '；';
                                    }
                                    $phase_array[] = $phase['text'];
                                }
                            }
                            $phase_data['text'] = $phase_array;
//                            $phase_data['text'] = $this->phaseTextModel->getPhaseText(['planet1_id'=>$planet[0],'planet2_id'=>$planet[1],'degrees'=>$degrees]);
                            $array['phase'][] = $phase_data;
                        }
                    }
                }
            }

            $data[] = $array;
        }
        $data = super_unique($data, true);

        $phase_texts = [];

        foreach ($data as $k => $v) {
            foreach ($v['phase'] as $kk => $vv) {
                if ($vv['text'] && $vv['name1'] == $v['planet']['name']) {
                    $phase_texts[$k]['text'][] = $vv['text'];
                } else {
                    $phase_texts[$k]['text'][] = '';
                }
            }
        }


    $data = ['link_status' => 1, 'data' => $data, 'phase_texts' => $phase_texts];
    return show(config('code.success'), '请求成功', $data);
    }
}
